package com.example.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import com.example.common.Result;
import org.apache.catalina.connector.ClientAbortException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 文件接口
 */
@RestController
@RequestMapping("/files")
public class FileController {

    // 文件上传存储路径
    private static final String filePath = System.getProperty("user.dir") + "/files/";

    @Value("${server.port:9090}")
    private String port;

    @Value("${ip:localhost}")
    private String ip;

    private static final Logger log = LoggerFactory.getLogger(FileController.class);


    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        String flag;
        synchronized (FileController.class) {
            flag = System.currentTimeMillis() + "";
            ThreadUtil.sleep(1L);
        }
        String fileName = file.getOriginalFilename();
        try {
            if (!FileUtil.isDirectory(filePath)) {
                FileUtil.mkdir(filePath);
            }
            // 文件存储形式：时间戳-文件名
            FileUtil.writeBytes(file.getBytes(), filePath + flag + "-" + fileName);  // ***/manager/files/1697438073596-avatar.png
            System.out.println(fileName + "--上传成功");

        } catch (Exception e) {
            System.err.println(fileName + "--文件上传失败");
        }
        String http = "http://" + ip + ":" + port + "/files/";
        return Result.success(http + flag + "-" + fileName);  //  http://localhost:9090/files/1697438073596-avatar.png
    }


    /**
     * 获取文件
     *
     * @param flag
     * @param response
     */
    @GetMapping("/{flag}")   //  1697438073596-avatar.png
    public void avatarPath(@PathVariable String flag, HttpServletResponse response) {
        OutputStream os;
        try {
            if (StrUtil.isNotEmpty(flag)) {
                response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(flag, "UTF-8"));
                response.setContentType("application/octet-stream");
                byte[] bytes = FileUtil.readBytes(filePath + flag);
                os = response.getOutputStream();
                os.write(bytes);
                os.flush();
                os.close();
            }
        } catch (Exception e) {
            System.out.println("文件下载失败");
        }
    }

    /**
     * 删除文件
     *
     * @param flag
     */
    @DeleteMapping("/{flag}")
    public void delFile(@PathVariable String flag) {
        FileUtil.del(filePath + flag);
        System.out.println("删除文件" + flag + "成功");
    }

    /**
     * 预览文件
     */
    @GetMapping("/preview/{filename}")
    public void previewFile(
            @PathVariable String filename,
            HttpServletResponse response) throws IOException {

        Path filePathObj = Paths.get(filePath + filename);
        File file = filePathObj.toFile();

        if (!file.exists()) {
            response.sendError(HttpStatus.NOT_FOUND.value(), "File not found");
            return;
        }

        // 动态获取Content-Type
        String contentType = Files.probeContentType(filePathObj);
        if (contentType == null) {
            contentType = "application/octet-stream";
        }

        // 设置响应头
        response.setContentType(contentType);
        response.setHeader("Content-Disposition",
                "inline; filename=" + URLEncoder.encode(filename, "UTF-8"));

        // 流式传输（关键修改）
        try (InputStream in = Files.newInputStream(filePathObj);
             OutputStream out = response.getOutputStream()) {

            byte[] buffer = new byte[4096]; // 4KB缓冲区
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                try {
                    out.write(buffer, 0, bytesRead);
                    out.flush(); // 确保及时发送
                } catch (ClientAbortException e) {
                    // 客户端断开时安全退出
                    log.debug("Client disconnected during transfer: {}", filename);
                    break;
                }
            }
        } catch (ClientAbortException e) {
            log.debug("Client aborted connection: {}", filename);
        }
    }


}
